package vn.vietel.pms.web.action;

import java.util.LinkedList;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import vn.vietel.pms.entity.VHistoricalAction;
import vn.vietel.pms.service.HistorialActionService;

@Scope(ScopeType.SESSION)
@Name("historicalAction")
public class HistoricalActionAction {
	@In(required = false)
	@Out(required = false)
	@DataModelSelection("historicalItems")
	private VHistoricalAction vhistoricalaction;

	@Logger
	Log log;
	@In("#{historicalActionService}")
	private HistorialActionService historicalActionService;

	@In
	private FacesMessages facesMessages;

	@DataModel
	private List<VHistoricalAction> historicalItems;

	private Long id;

	private boolean editcompleted;

	private boolean editting;

	private String action;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		editcompleted = false;
		editting = false;
		if (id != null) {
			vhistoricalaction = historicalActionService.find(id);
			editting = (vhistoricalaction != null);
		} else {
			vhistoricalaction = new VHistoricalAction();
			editting = false;
		}
	}

	public void createItem() {
		try {
			editcompleted = false;
			historicalActionService.create(vhistoricalaction);
			getHistoricalItems();
			editcompleted = true;
		} catch (IllegalStateException e) {
			facesMessages.add("Opps!!! Could not insert....");
			facesMessages.add(e.getMessage());
			editcompleted = false;
		}
	}

	public void updateItem() {
		try {
			editcompleted = false;
			historicalActionService.update(vhistoricalaction);
			getHistoricalItems();
			editcompleted = true;
			editting = false;

		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	public void deleteItem(ActionEvent ae) {
		try {
			Long rId = vhistoricalaction.getId();
			VHistoricalAction rgp = historicalActionService.find(rId);
			historicalActionService.delete(rgp);
			getHistoricalItems();
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	@Factory()
	public List<VHistoricalAction> getHistoricalItems() {
		historicalItems = historicalActionService.getAll();
		if (historicalItems == null)
			historicalItems = new LinkedList<VHistoricalAction>();
		return historicalItems;
	}

	public void setHistoricalItems(List<VHistoricalAction> items) {
		this.historicalItems = items;
	}

	public boolean isEditcompleted() {
		return editcompleted;
	}

	public void setEditCompleted(boolean editcompleted) {
		this.editcompleted = editcompleted;
	}

	public boolean isEditting() {
		return editting;
	}

	public void setEditting(boolean editting) {
		this.editting = editting;
	}

	public boolean notEditing() {
		return !editting;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

}
